package com.chinatelecom.epush.im;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.push.example.MainActivity;
import com.chinatelecom.epush.R;
import com.chinatelecom.epush.im.MainTabFriends.OnUnReadMessageUpdateListener;
import com.chinatelecom.epush.im.client.PushMessageReceiver;
import com.chinatelecom.epush.widge.AppManager;
import com.chinatelecom.epush.widge.BadgeView;

public class MainActivity1 extends FragmentActivity implements
		OnUnReadMessageUpdateListener
{
	private ViewPager mViewPager;
	private FragmentPagerAdapter mAdapter;
	private List<Fragment> mFragments = new ArrayList<Fragment>();
	/**
	 * 
	 */
	private LinearLayout mTabLiaotian;

	/**
	 *
	 */
	private TextView mLiaotian;
	private TextView mFaxian;
	private TextView mTongxunlu;

	/**
	 * 
	 */
	private BadgeView mBadgeViewforLiaotian;

	/**
	 * Fragment
	 */
	private MainTabFriends mFrined;

	/**
	 * 
	 */
	private ImageView mTabLine;
	/**
	 * 
	 */
	private int currentIndex;
	/**
	 *
	 */
	private int screenWidth;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initTabLine();
		/**
		 * ��ʼ��Adapter
		 */
		mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
		{
			@Override
			public int getCount()
			{
				return mFragments.size();
			}

			@Override
			public Fragment getItem(int arg0)
			{
				return mFragments.get(arg0);
			}
		};
		mViewPager.setAdapter(mAdapter);

		

		mViewPager.setCurrentItem(0);


	}
	
	/**
	 * �����Ļ�Ŀ�ȣ���ʼ�����ߵĿ��
	 */
	private void initTabLine()
	{
		mTabLine = (ImageView) findViewById(R.id.id_tab_line);
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay()
				.getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) mTabLine
				.getLayoutParams();
		lp.width = screenWidth / 3;
		mTabLine.setLayoutParams(lp);
	}

//	/**
//	 * ������ɫ
//	 */
//	protected void resetTextView()
//	{
//		mLiaotian.setTextColor(getResources().getColor(R.color.black));
//		mFaxian.setTextColor(getResources().getColor(R.color.black));
//		mTongxunlu.setTextColor(getResources().getColor(R.color.black));
//	}
//
//	/**
//	 * ��ʼ���ؼ�����ʼ��Fragment
//	 */
	private void initView()
	{
		mViewPager = (ViewPager) findViewById(R.id.id_viewpager);

		mTabLiaotian = (LinearLayout) findViewById(R.id.id_tab_liaotian_ly);

		mLiaotian = (TextView) findViewById(R.id.id_liaotian);
//		mFaxian = (TextView) findViewById(R.id.id_faxian);
//		mTongxunlu = (TextView) findViewById(R.id.id_tongxunlu);

		mFrined = new MainTabFriends();
//		MainTab02 tab02 = new MainTab02();
//		MainTab03 tab03 = new MainTab03();
		mFragments.add(mFrined);
//		mFragments.add(tab02);
//		mFragments.add(tab03);

		mBadgeViewforLiaotian = new BadgeView(this);
		mTabLiaotian.addView(mBadgeViewforLiaotian);
		mBadgeViewforLiaotian.setVisibility(View.GONE);
	}

	/**
	 * update modified
	 */
	@Override
	protected void onResume()
	{
		super.onResume();

		// ���֪ͨ����������
		if (PushMessageReceiver.mNewNum > 0)
		{
			// ���֪ͨ
			PushApplication.getInstance().getNotificationManager()
					.cancel(PushMessageReceiver.NOTIFY_ID);
			PushMessageReceiver.mNewNum = 0;
		}
	}

	/**
	 * update modified
	 */
	@Override
	public void unReadMessageUpdate(int count)
	{
		if (count == 0 && mBadgeViewforLiaotian.getBadgeCount() == 0)
			mBadgeViewforLiaotian.setVisibility(View.GONE);
		mBadgeViewforLiaotian.setVisibility(View.VISIBLE);
		mBadgeViewforLiaotian.setBadgeCount(count);

	}
	
	//菜单栏
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        /*
         * 
         * add()方法的四个参数，依次是：
         * 
         * 1、组别，如果不分组的话就写Menu.NONE,
         * 
         * 2、Id，这个很重要，Android根据这个Id来确定不同的菜单
         * 
         * 3、顺序，那个菜单现在在前面由这个参数的大小决定
         * 
         * 4、文本，菜单的显示文本
         */

        menu.add(Menu.NONE, Menu.FIRST + 1, 5, "清除").setIcon(

        android.R.drawable.ic_menu_delete);

        // setIcon()方法为菜单设置图标，这里使用的是系统自带的图标，同学们留意一下,以

        // android.R开头的资源是系统提供的，我们自己提供的资源是以R开头的

        menu.add(Menu.NONE, Menu.FIRST + 2, 2, "绑定").setIcon(

        android.R.drawable.ic_menu_edit);

        menu.add(Menu.NONE, Menu.FIRST + 3, 6, "退出").setIcon(

        android.R.drawable.ic_lock_power_off);

        menu.add(Menu.NONE, Menu.FIRST + 4, 1, "主页").setIcon(

        android.R.drawable.ic_menu_add);

        menu.add(Menu.NONE, Menu.FIRST + 5, 4, "刷新").setIcon(

        android.R.drawable.ic_menu_info_details);

        menu.add(Menu.NONE, Menu.FIRST + 6, 3, "帮助").setIcon(

        android.R.drawable.ic_menu_help);

        return true;

    }
    //菜单选项被选中时事件
    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
		
            switch (item.getItemId()) {

            case Menu.FIRST + 1:
            	Toast.makeText(this, "开源无限,后续扩展!", Toast.LENGTH_LONG).show(); 
                 

                break;

            case Menu.FIRST + 2:
            	 
               Toast.makeText(this, "开源无限,后续扩展!", Toast.LENGTH_LONG).show();

                break;

            case Menu.FIRST + 3:
            	AppManager appman = AppManager.getAppManager();
                appman.AppExit(MainActivity1.this);
                
                break;

            case Menu.FIRST + 4:
            	 Intent intentPush = new Intent();
                 intentPush.setClass(MainActivity1.this, MainActivity.class);
                 startActivity(intentPush);
            	          
                break;

            case Menu.FIRST + 5:
            	Toast.makeText(this, "开源无限,后续扩展!", Toast.LENGTH_LONG).show();
               

                break;

            case Menu.FIRST + 6:
            	Toast.makeText(this, "开源无限,后续扩展!", Toast.LENGTH_LONG).show();             
                break;

            }

            return false;

        }

}
